/**
 * Zhznet.com Inc.
 * Copyright (c) 2004-2011 All Rights Reserved.
 */
package com.zhz.project.main.socolar.wenzhang.util;

import java.io.File;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.MultipartPostMethod;

/**
 * 用来演示提交文件的例子
 * 
 * @author Liwei
 * @version $Id: PostXMLClient.java, v 0.1 2011-1-22 下午09:24:34 Liwei Exp $
 */
public class PostFileToServerClient {

	private static String targetURL = "http://202.117.24.217:80/OCRWeb/SocolarCaptcha";

	public String getTxtCode(String targetFilePath) throws Exception {
		// System.out.println(targetFilePath);
		MultipartPostMethod filePost = new MultipartPostMethod(targetURL);

		filePost.addParameter("userfile", new File(targetFilePath));

		filePost.addParameter("senderid", "test");

		HttpClient client = new HttpClient();

		// 由于要上传的文件可能比较大 , 因此在此设置最大的连接超时时间
		client.getHttpConnectionManager().getParams()
				.setConnectionTimeout(5000);

		client.executeMethod(filePost);

		String code = filePost.getResponseBodyAsString();

		filePost.releaseConnection();
		return code;
	}

}
